package com.cinc.ebc.sd.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cinc.ebc.sd.entity.SdContract;
import com.cinc.ebc.sd.entity.SdContractDetail;
import com.cinc.ebc.sd.enums.ContractStatusEnum;
import com.cinc.ebc.sd.mapper.SdContractDetailMapper;
import com.cinc.ebc.sd.mapper.SdContractMapper;
import com.cinc.ebc.sd.service.ISdContractService;
import org.jeecg.common.util.oConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;

/**
 * @Description: 销售合同
 * @Author: jeecg-boot
 * @Date:   2022-08-01
 * @Version: V1.0
 */
@Service
public class SdContractServiceImpl extends ServiceImpl<SdContractMapper, SdContract> implements ISdContractService {

	@Autowired
	private SdContractMapper sdContractMapper;
	@Autowired
	private SdContractDetailMapper sdContractDetailMapper;

	@Override
	@Transactional
	public void saveMain(SdContract sdContract, List<SdContractDetail> sdContractDetailList) {
		sdContractMapper.insert(sdContract);

		if(sdContractDetailList!=null && sdContractDetailList.size()>0) {
			BigDecimal sumPrice = BigDecimal.ZERO;
			for(SdContractDetail entity:sdContractDetailList) {
				//外键设置
				entity.setPid(sdContract.getId());
				sdContractDetailMapper.insert(entity);
				//合同金额累计
				sumPrice = sumPrice.add(entity.getSumPrice() == null ? BigDecimal.ZERO : entity.getSumPrice());
			}
			//更新主表金额
			SdContract contractModify = new SdContract();
			contractModify.setId(sdContract.getId());
			contractModify.setMoney(sumPrice);
			sdContractMapper.updateById(contractModify);
		}

	}

	@Override
	@Transactional
	public void updateMain(SdContract sdContract,List<SdContractDetail> sdContractDetailList) {
		sdContractMapper.updateById(sdContract);

		//1.先删除子表数据
		sdContractDetailMapper.deleteByMainId(sdContract.getId());

		//2.子表数据重新插入
		if(sdContractDetailList!=null && sdContractDetailList.size()>0) {
			BigDecimal sumPrice = BigDecimal.ZERO;
			for(SdContractDetail entity:sdContractDetailList) {
				//外键设置
				entity.setPid(sdContract.getId());
				sdContractDetailMapper.insert(entity);
				//合同金额累计
				sumPrice = sumPrice.add(entity.getSumPrice() == null ? BigDecimal.ZERO : entity.getSumPrice());
			}
			//更新主表金额
			SdContract contractModify = new SdContract();
			contractModify.setId(sdContract.getId());
			contractModify.setMoney(sumPrice);
			sdContractMapper.updateById(contractModify);
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		sdContractDetailMapper.deleteByMainId(id);
		sdContractMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			sdContractDetailMapper.deleteByMainId(id.toString());
			sdContractMapper.deleteById(id);
		}
	}

	/**
	 * 自动更新合同信息
	 * @param id
	 */
	@Transactional
	@Override
	public void autoUpdateContract(String id) {
		SdContract sdContract = sdContractMapper.selectById(id);
		if(ContractStatusEnum.TYPE01.getCode().equals(sdContract.getStatus())){
			return;
		}
		// 更新合同明细数量
		List<SdContractDetail> sdContractDetailList = sdContractDetailMapper.selectByMainId(id);
		if(oConvertUtils.listIsEmpty(sdContractDetailList)){
			return;
		}
		int salesCount = 0;
		int finishCount = 0;
		for(SdContractDetail sdContractDetail : sdContractDetailList){
			sdContractDetailMapper.autoUpdateSalesQtyByPrimaryKey(sdContractDetail.getId());
			sdContractDetailMapper.autoUpdateDeliveryQtyByPrimaryKey(sdContractDetail.getId());
			if(sdContractDetail.getSalesQty().compareTo(BigDecimal.ZERO) == 1){
				salesCount += 1;
			}
			if(sdContractDetail.getDeliveryQty().compareTo(sdContractDetail.getQty()) >= 0){
				finishCount += 1;
			}
		}
		// 更新合同状态
		SdContract contractModify = new SdContract();
		contractModify.setId(id);
		if(finishCount == sdContractDetailList.size()){
			contractModify.setStatus(ContractStatusEnum.TYPE04.getCode());
		}else if(salesCount > 0){
			contractModify.setStatus(ContractStatusEnum.TYPE03.getCode());
		}else{
			contractModify.setStatus(ContractStatusEnum.TYPE02.getCode());
		}
		sdContractMapper.updateById(contractModify);
	}

}
